package hjt.com.module.user.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by hjt on 2019/4/9.
 */

public class Clock extends View {

    private int mWidth;
    private int mHeight;

    public Clock(Context context) {
        super(context);
    }

    public Clock(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Clock(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public Clock(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        paint.setAntiAlias(true);

        canvas.translate(mWidth / 2, mHeight / 2);
        //圆心
        paint.setColor(Color.RED);
        canvas.drawPoint(0, 0, paint);
        //外圆
        paint.setColor(Color.GREEN);
        canvas.drawCircle(0, 0, mWidth / 2, paint);

        for (int i = 0; i < 12; i++) {
            if (i == 0 || i == 3 || i == 6 || i == 9) {
                paint.setStrokeWidth(5);
                paint.setColor(Color.BLACK);
                canvas.drawLine(0, -mWidth / 2, 0, -mWidth / 2 + 30, paint);
                canvas.save();
                canvas.translate(0, -mWidth / 2 + 60);
                canvas.rotate(-30 * i);
                paint.setTextSize(26);
                paint.setColor(Color.RED);
                paint.setStrokeWidth(3);
                canvas.drawText(String.valueOf(i), 0 - paint.measureText(String.valueOf(i)) / 2, 0, paint);
                canvas.restore();
//                canvas.drawText(String.valueOf(i), -paint.measureText(String.valueOf(i)) / 2, -mWidth / 2 + 60, paint);
            }
            canvas.rotate(30, 0, 0);
        }
    }
}
